package com.huarui.companypackage.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.ewaf.framework.action.BaseAction;

import com.huarui.company.model.Company;
import com.huarui.company.service.CompanyService;
import com.huarui.companypackage.service.PackageService;
import com.huarui.quotation.service.QuotationService;
import com.huarui.user.model.SessionMessage;
import com.huarui.companypackage.model.Package;
import com.huarui.user.security.UserHolder;
import com.huarui.util.ConstantUtil;

public class PackageAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1018162253168903437L;
	private Logger log = Logger.getLogger(PackageAction.class);
	private String queryParam;
	private PackageService packageService;
	private CompanyService companyService;
	private SessionMessage sessionMessage;
	private QuotationService quotationService;
	private Map<String, Object> jsonResult;

	// 分页
	private Integer start;
	private Integer limit;
	private String sort;
	private String dir;
	public String addPackage(){
		jsonResult = new HashMap<String, Object>();
		try {
			packageService.createPackage(queryParam);
			jsonResult.put("success", true);
			jsonResult.put("msg", "保存套餐成功!");
		} catch (Exception e) {
			log.error("保存套餐失败......",e);
			jsonResult.put("success", true);
			jsonResult.put("msg", "保存套餐失败......");
		}						
		return SUCCESS;
	}
	
	public String searchPackage(){
		sessionMessage = UserHolder.getCurrentSessionMessage();		
		jsonResult = new HashMap<String, Object>();
		try {
			List<Package> packages = packageService.getPackagesByCompanyType(sessionMessage.getCompanyType(),Package.IS_GIFT_NO);
			Company company = (Company) companyService.getById(sessionMessage.getCompanyId());
			jsonResult.put("companyType", sessionMessage.getCompanyType());
			jsonResult.put("balance", company.getBalance());
			jsonResult.put("msg", "加载套餐列表成功......");
			jsonResult.put("success", true);
			jsonResult.put("root", packages);
		} catch (Exception e) {
			log.error("加载套餐列表失败......",e);
			jsonResult.put("success", false);
			jsonResult.put("msg", "加载套餐列表失败......");
		}
		return SUCCESS;
	}
	/**
	 * 浏览套餐
	 * @return
	 */
	public String searchAllPackage(){
		
		try {
			jsonResult =queryMapParamUtil();
			jsonResult = packageService.getAllPackages(jsonResult);
			jsonResult.put("success", true);
		} catch (Exception e) {
			log.error("加载套餐列表失败......",e);
			jsonResult.put("success", false);
			jsonResult.put("msg", "加载套餐列表失败......");
		}
		return SUCCESS;
	}
	
	public String buyPackage(){
		jsonResult = new HashMap<String, Object>();
		try {
			jsonResult = packageService.buyPackage(queryParam,httpServletRequest.getRemoteAddr());
			if(jsonResult.get("success")!=null && jsonResult.get("success").toString().equals("true")){
				System.out.println("同步报价");
				sessionMessage = UserHolder.getCurrentSessionMessage();	
				quotationService.batchOperationQuotation(sessionMessage.getCompanyId());
			}
			
		} catch (Exception e) {
			log.error("购买套餐失败......",e);
			jsonResult.put("success", false);
			jsonResult.put("msg", "购买套餐失败......");
		}
		return SUCCESS;
	}
	
	private Map<String,Object> queryMapParamUtil() {
		jsonResult = new HashMap<String, Object>();
		jsonResult.put("start",start ==null ? 0: start);
		jsonResult.put("pageSize",limit == null ? 20: limit);
		jsonResult.put("orderBy", sort == null ? "code":sort);
		
		if (dir != null){
			jsonResult.put("desc", dir.equals(ConstantUtil.DESC) ? true : false);			
		}else{
			jsonResult.put("desc",true);	
			
		}		
		return jsonResult;
	}
	
	public void setQueryParam(String queryParam) {
		this.queryParam = queryParam;
	}

	public void setPackageService(PackageService packageService) {
		this.packageService = packageService;
	}

	public Map<String, Object> getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(Map<String, Object> jsonResult) {
		this.jsonResult = jsonResult;
	}

	public void setSessionMessage(SessionMessage sessionMessage) {
		this.sessionMessage = sessionMessage;
	}

	public Integer getStart() {
		return start;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public Integer getLimit() {
		return limit;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}
	public void setQuotationService(QuotationService quotationService) {
		this.quotationService = quotationService;
	}
}
